package com.jy.app.lib.utils;

import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.jy.app.lib.application.BaseApplication;

import java.lang.ref.WeakReference;

/**
 * 
 * TODO 提示框工具类
 * <p/>
 * 创建时间: 2015年3月19日 下午11:46:54 <br/>
 * 
 * @author xszhang
 * @version
 * @since v0.0.1
 */
public class ToastUtils {

	public static void toastShort(String text) {
		toastShort(BaseApplication.appContext, text);
	}
	
	public static void toastLong(String text) {
		toastLong(BaseApplication.appContext, text);
	}
	
	public static void toastShort(Context context, String text) {
		ToastShowManager.getInstance().showToastLong(context, text);
	}

	public static void toastShort(Context context, int resId) {
		ToastShowManager.getInstance().showToast(context, resId);
	}

	public static void toastLong(Context context, String text) {
		ToastShowManager.getInstance().showToastLong(context, text);
	}

	public static void toastLong(Context context, int resId) {
		ToastShowManager.getInstance().showToastLong(context, resId);
	}

	public static void toast(Context context, String text, int duration) {
		ToastShowManager.getInstance().showToast(context, text, duration);
	}

	public static void toast(Context context, int resId, int duration) {
		if (context != null) {
			String text = context.getResources().getString(resId);
			ToastShowManager.getInstance().showToast(context, text, duration);
		}
	}

	private static Toast toastPublic;
	private static TextView textViewToast;

	/**
	 * 关闭所有的提示Toast信息框
	 * 
	 * @author xnjiang
	 * @since v0.0.1
	 */
	public static void hideToastImage() {
		if (toastPublic != null) {
			toastPublic.cancel();
		}
	}

	/**
	 * 显示带文字和图片的Toast
	 * 
	 * @param context
	 * @param strMessage
	 * @param resid
	 */
	public static void showToastImage(Context context, String strMessage, int resid) {

		if (textViewToast == null) {
			textViewToast = new TextView(context);
//			textViewToast.setBackgroundResource(R.drawable.roundcorner_black);
			int intSet = 20;
			textViewToast.setPadding(intSet, intSet, intSet, intSet);
			// textViewToast.layout(intSet, intSet, intSet, intSet);

			textViewToast.setLayoutParams(new LayoutParams(DensityUtil.dip2px(context, 100), LayoutParams.WRAP_CONTENT));
			textViewToast.setGravity(Gravity.CENTER);
			textViewToast.setTextSize(20f);
			textViewToast.setTextColor(Color.WHITE);
			textViewToast.setLineSpacing(1.5f, 1.5f);
			textViewToast.setCompoundDrawablesWithIntrinsicBounds(0, resid, 0, 0);
		}
		textViewToast.setText(strMessage);
		if (toastPublic == null) {
			toastPublic = new Toast(context);
			toastPublic.setGravity(Gravity.CENTER, 0, 0);
			toastPublic.setDuration(Toast.LENGTH_SHORT);
		}
		toastPublic.setView(textViewToast);
		toastPublic.show();
	}

	private static class ToastShowManager {

		private static final String TAG = ToastShowManager.class.getSimpleName();

		private static volatile ToastShowManager mInstance = null;
		private ToastInfo mPreShowToastInfo;
		/**
		 * 两个相同的Toast显示的时间间隔,以毫秒为单位.
		 */
		private final long mShowSameToastInterval = 2500;

		public ToastShowManager() {
		}

		public static ToastShowManager getInstance() {
			if (mInstance == null) {
				synchronized (ToastShowManager.class) {
					if (mInstance == null) {
						mInstance = new ToastShowManager();
					}
				}
			}
			return mInstance;
		}

		public void showToast(Context context, int resId) {
			if (context != null) {
				String text = context.getString(resId);
				showToast(context, text);
			}
		}

		public void showToastLong(Context context, int resId) {
			if (context != null) {
				String text = context.getString(resId);
				showToastLong(context, text);
			}
		}

		public void showToast(Context context, String text) {
			showToast(context, text, Toast.LENGTH_SHORT);
		}

		public void showToastLong(Context context, String text) {
			showToast(context, text, Toast.LENGTH_LONG);
		}

		public void showToast(Context context, String text, int duration) {
			if (mPreShowToastInfo == null) {
//				updateToastInfoAndShowToast(context, text, duration);
			} else {
				if (!TextUtils.isEmpty(mPreShowToastInfo.text) && mPreShowToastInfo.text.equals(text)) {
					/*
					 * 当接下来要显示的Toast的文本与先前的相同时,按照以下规则进行显示. #1>
					 * 如果两个Toast之间的时间间隔大于预定义的时间间隔,则显示第二个Toast. #2>
					 * 如果两个Toast的Context不相同时,则显示第二个Toast. #3> 如果不满足1,2则只更新时间.
					 */
					final Context preToastContext = mPreShowToastInfo.contextRef.get();
					if ((System.currentTimeMillis() - mPreShowToastInfo.showTime > mShowSameToastInterval) || (preToastContext != context)) {
//						updateToastInfoAndShowToast(context, null, duration);
					} else {
						// 仅仅只更新保存的Toast的时间.
						mPreShowToastInfo.showTime = System.currentTimeMillis();
						LogUtil.d("update show time: " + mPreShowToastInfo.showTime);
					}
				} else {
//					updateToastInfoAndShowToast(context, text, duration);
				}
			}
			updateToastInfoAndShowToast(context, text, duration);
		}

		/**
		 * 更新保存的Toast信息,然后显示Toast.
		 * 
		 * @param newContext
		 * @param newText
		 * @param duration
		 */
		private void updateToastInfoAndShowToast(Context newContext, String newText, int duration) {
			if (mPreShowToastInfo == null) {
				mPreShowToastInfo = new ToastInfo();
			}

			if (newText != null) {
				mPreShowToastInfo.text = newText;
			}

			if (newContext != null) {
				mPreShowToastInfo.contextRef = new WeakReference<Context>(newContext);
			}
			Toast mToast = null;
			try {
				mToast = Toast.makeText(newContext, null, duration);
				mToast.setText(mPreShowToastInfo.text);
				mToast.show();
			}catch (Exception e){
				Looper.prepare();
				mToast = Toast.makeText(newContext, null, duration);
				mToast.setText(mPreShowToastInfo.text);
				mToast.show();
				Looper.loop();
			}
			mPreShowToastInfo.showTime = System.currentTimeMillis();
			LogUtil.d("update show time: " + mPreShowToastInfo.showTime);
		}

		private static class ToastInfo {
			WeakReference<Context> contextRef;
			String text;
			long showTime;
		}
	}
}
